import requests
import re
import pymongo

URL = 'http://maoyan.com/board/4'
headers = {
    'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8',
    'Accept-Encoding': 'gzip, deflate',
    'Accept-Language': 'zh-CN,zh;q=0.9',
    'Cache-Control': 'max-age=0',
    'Connection':'keep-alive',
    'Cookie': '_lxsdk_cuid=16421b88726c8-09d1b85e76e2d6-5b183a13-100200-16421b88727c8;' \
        ' uuid=1A6E888B4A4B29B16FBA1299108DBE9C64C86ED' \
        '48A13A0A4BC0D1ECE7A6C4346; _csrf=0d4cf08f9b428d7587f8873b83e2831ef5b' \
        '46ac0ec1f2bcf6949f5a0786ff159; _lx_utm=utm_source%3DBaidu%26utm_medium%3Dorganic; ' \
        '_lxsdk=1A6E888B4A4B29B16FBA1299108DBE9C64C86ED48A13A0A4BC0D1ECE7A6C4346;' \
        ' __mta=55492116.1529574169508.1529574509527.1529574599664.7;' \
        ' _lxsdk_s=16421b88729-2d1-74d-d08%7C%7C23',
    'Host': 'maoyan.com',
    'Referer': 'http://maoyan.com/board',
    'Upgrade-Insecure-Requests': '1',
    'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko)'\
                  ' Chrome/67.0.3396.87 Safari/537.36',

}

MONGO_URL='localhost'
MONGO_DB='Maoyan'
MONGO_TABLE='mytop100'
client = pymongo.MongoClient(MONGO_URL)
db = client[MONGO_DB]
myset = db[MONGO_TABLE]


def get_one_page(url,param):
    response = requests.get(url=url,headers=headers,params=param)
    return response.text


def save_to_mongo(movie):
    if myset.insert(movie):
        print('插入成功',movie)
    else:
        print('插入失败',movie)


def get_moive_info(html):
    pattern = re.compile(r'<dd>.*?title="(?P<name>.*?)".*?主演：(?P<actor>.*?)</p>.*?上映时间：(?P<time>.*?)</p>.*?integer">'
                         r'(?P<pre>.*?)</i>.*?fraction">(?P<end>.*?)</i>',re.S)
    resluts = re.findall(pattern,html)
    for info in resluts:
        movie = {}
        movie['name'] = info[0]
        movie['actor'] = info[1].strip()
        movie['time'] = info[2].strip()
        movie['score'] = info[3]+info[4]
        save_to_mongo(movie)


def main():
    for i in range(10):
        page = i * 10
        param = {
            'offset':page,
        }
        html = get_one_page(URL,param=param)
        get_moive_info(html)


if __name__ == '__main__':
    main()    